package ua.maxicash.util.criteria.handlers;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;
import ua.maxicash.util.Filter;
import ua.maxicash.util.criteria.RestrictionsWrapper;

import java.util.List;

public class RegionCriteriaHandler extends CriteriaHandler {
    private Filter filter;

    public RegionCriteriaHandler() {
    }

    public RegionCriteriaHandler(List<String> aliases) {
        super(aliases);
    }

    @Override
    public void setUp(Filter filter) {
        this.filter = filter;
    }

    @Override
    public DetachedCriteria handle(DetachedCriteria criteria, Junction junction) {

        if (!aliases.contains("region")) {
            criteria.createAlias("city.region", "region");
            aliases.add("region");
        }

        junction.add(Restrictions.or(RestrictionsWrapper.getCriterion(
                filter.getOp(), "region.ruName", filter.getData()),
                RestrictionsWrapper.getCriterion(filter.getOp(),
                        "region.enName", filter.getData())));

        return criteria;
    }
}
